//
//  MainTabBarViewController.m
//  iOSProjectExtension
//
//  Created by Apple on 2020/1/10.
//  Copyright © 2020 lidianchao. All rights reserved.
//

#import "MainTabBarViewController.h"
#import "MainTabbarManager.h"
#import "Config.h"
@interface MainTabBarViewController ()

@end

@implementation MainTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoutViewControllers];
    // Do any additional setup after loading the view.
}

///创建Controllers
-(void)layoutViewControllers
{
    [[MainTabbarManager sharedManager] getAllNavigationControllers:^(NSArray *allNavs, NSArray *viewArr) {
        self.viewControllers = allNavs;
    }];

}
//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
    UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    return currentVC;
}

- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
    UIViewController *currentVC;
    if ([rootVC presentedViewController]) {
        // 视图是被presented出来的
        rootVC = [rootVC presentedViewController];
        if ([currentVC isKindOfClass:[UIViewController class]]) {
            currentVC = rootVC;
        }
    }
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        // 根视图为UITabBarController
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        // 根视图为UINavigationController
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
    } else {
        // 根视图为非导航类
        currentVC = rootVC;
    }
    return currentVC;
}

- (void)LogutPopRootViewController {
    
    [[self getCurrentVC].navigationController popToRootViewControllerAnimated:NO];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
